<?php

namespace App\Http\Controllers\user;

use Illuminate\Support\Facades\Session;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use \App\User;
class RegisterController extends Controller
{
    //
    public function index(){
        return view('user.register');
    }
    public function register(){
        try {
            //验证
            $this->validate(request(),[
                'name' => 'required|min:5|max:30|unique:users,name',
                'email' => 'required|email|unique:users,email',
                'password' => 'required|min:5|max:20|confirmed',
            ]);
            //逻辑
            $name = request('name');
            $email = request('email');
            $password = bcrypt(request('password'));
            $user = new User();
            $user->name = $name;
            $user->email = $email;
            $user->password = $password;
            $user->save();
            return redirect('/login');
        } catch (QueryException $e) {
            return redirect('/posts')->withErrors('注册失败！');
        }
    }
}
